﻿using NuGet.Protocol.Core.Types;
using ProjectCore.Domain.DomainService;
using ProjectCore.Domain.Model.Entity;
using ProjectCore.Domain.Repository.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace ProjectCore.Application
{
    public class AppRoles : IAppRoles
    {
        private readonly IRolesDomainService rolesDomainService;
        private readonly IBaseRepository<RoleMenus> baseRepository1;
        public AppRoles(IRolesDomainService _rolesDomainService, IBaseRepository<RoleMenus> _baseRepository1)
        {
            rolesDomainService = _rolesDomainService;
            baseRepository1 = _baseRepository1;
        }
        public async Task<int> Add(Roles roles)
        {
            return await rolesDomainService.Add(roles);
        }

        public async Task<List<Roles>> GetRoles()
        {
            return await rolesDomainService.GetRoles();
        }

        public async Task<int> IsDel(int Id)
        {
            return await rolesDomainService.IsDel(Id);
        }

        public async Task<int> Upt(Roles roles)
        {
            return await rolesDomainService.Upt(roles);
        }
        public async Task<Roles> Edit(int Id)
        {
            return await rolesDomainService.Edit(Id);
        }



        public async Task<int> UptRoleMenus(List<int> vs, int Id)
        {
            Expression<Func<RoleMenus, bool>> where = e => e.RoleID.Equals(Id);
            await baseRepository1.WhereLoadEntityAsync(where);

            List<RoleMenus> roleMenus = new List<RoleMenus>();
            for (int i = 0; i < vs.Count-1; i++)
            {
                RoleMenus roleMenus1 = new RoleMenus();
                roleMenus1.MenuID = vs[i];
                roleMenus1.RoleID = Id;
                roleMenus1.Add(roleMenus);
            }
            return await baseRepository1.AddRangeListAsync(roleMenus);
        }

       public async Task<List<RoleMenus>> GetRoleMenus(int Id)
        {
            try
            {
                Expression<Func<RoleMenus, bool>> where = e => e.RoleID.Equals(Id);
                return await baseRepository1.WhereLoadEntityList(where);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
